/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.lifxue.jqda.ui;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;
import javax.swing.SwingUtilities;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.lifxue.jqda.data.DataUtil;
import org.lifxue.jqda.jintellitype.GlobalHotkeyResourceManagement;

/**
 *
 * @author lifxue
 */
public class TrayPopupMenu {

    private static Log logger = LogFactory.getLog(TrayPopupMenu.class);
    public static PopupMenu pm;
    public static Desktop desktop;
    public static SystemTray st;

    public void tray() {
        if (Desktop.isDesktopSupported()) {//判断当前平台是否支持Desktop类
            desktop = Desktop.getDesktop();
        }
        if (SystemTray.isSupported()) {//判断当前平台是否支持系统托盘
            st = SystemTray.getSystemTray();
            URL url = this.getClass().getResource("butterfly16.png");
            Image image = Toolkit.getDefaultToolkit().getImage(url);
            createPopupMenu();//创建菜单
            TrayIcon ti = new TrayIcon(image, "【ALT+空格】打开快速启动框", pm);
            try {
                st.add(ti);
            } catch (AWTException ex) {
            }
        }
    }

    private void createPopupMenu() {

        pm = new PopupMenu();
        MenuItem keyWords = new MenuItem("热键设置");
        keyWords.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                SwingUtilities.invokeLater(new Runnable() {

                    @Override
                    public void run() {
                        HotKeyJDialog hk = HotKeyJDialog.getInstance(true);
                        hk.setVisible(true);
                    }
                });
            }
        });

        MenuItem setItem = new MenuItem("快速启动设置");
        setItem.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                SwingUtilities.invokeLater(new Runnable() {

                    @Override
                    public void run() {
                        QuickStartListDialog QSList = QuickStartListDialog.getInstance(true);
                        QSList.setVisible(true);

                    }
                });
            }
        });

        MenuItem lookandfeelItem = new MenuItem("界面风格设置");
        lookandfeelItem.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                SwingUtilities.invokeLater(new Runnable() {

                    @Override
                    public void run() {
                        LookAndFeelJDialog laf = LookAndFeelJDialog.getInstance(true);
                        laf.setVisible(true);
                    }
                });
            }
        });

        MenuItem showAppFrame = new MenuItem("显示快速启动框");
        showAppFrame.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                openAppframe();
            }
        });
        
        MenuItem aboutMenu = new MenuItem("关于...");
        aboutMenu.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                SwingUtilities.invokeLater(new Runnable() {

                    @Override
                    public void run() {
                        AboutJDialog about = AboutJDialog.getInstance(true);
                        about.setVisible(true);
                    }
                });
            }
        });

        MenuItem exitMenu = new MenuItem("退出程序");
        exitMenu.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                GlobalHotkeyResourceManagement.releaseResources();
                DataUtil.updateAppsFile();
                DataUtil.updateConfigFile();
                System.exit(0);
            }
        });
        pm.add(showAppFrame);
        pm.addSeparator();
        pm.add(keyWords);
        pm.add(setItem);
        pm.add(lookandfeelItem);
        pm.addSeparator();
        pm.add(aboutMenu);
        pm.add(exitMenu);
    }

    //打开快速启动框
    private void openAppframe() {
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                SwingUtilities.invokeLater(new Runnable() {

                    @Override
                    public void run() {
                        QuickStartJFrame qs = QuickStartJFrame.getInstance();
                        qs.setVisible(true);
                    }
                });
            }
        });

    }
}
